package com.flute.icrawler.framework.framework.container;

import java.io.Serializable;
import java.util.LinkedHashMap;

public class AdditionalUrlInformation implements
		Comparable<AdditionalUrlInformation>, Serializable {

	public enum Additionals {
		Deep, Position, UpdateCount, RetryCount, ResourceID
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -429885823598035188L;

	private LinkedHashMap<Additionals, ComparableFeature> featureList;

	public AdditionalUrlInformation() {
		this.featureList = new LinkedHashMap<Additionals, ComparableFeature>();
		initialFeatures();
	}

	/**
	 * 排序方式的定义
	 */
	private void initialFeatures() {
		this.featureList.put(Additionals.UpdateCount, new ComparableFeature(
				Additionals.UpdateCount.name(), new NumberStringValue(0)));
		this.featureList.put(Additionals.Deep, new ComparableFeature(
				Additionals.Deep.name(), new NumberStringValue(0)));
		this.featureList.put(Additionals.Position, new ComparableFeature(
				Additionals.Position.name(), new NumberStringValue(0)));
		this.featureList.put(Additionals.RetryCount, new ComparableFeature(
				Additionals.RetryCount.name(), new NumberStringValue(0)));
		this.featureList.put(Additionals.ResourceID, new ComparableFeature(
				Additionals.ResourceID.name(), new NumberStringValue(0)));

	}

	public int getFeatureCount() {
		return featureList.size();
	}

	public IStringValue getValue(Additionals featureName) {
		return featureList.get(featureName).getValue();
	}

	public void updateValue(Additionals featureName, IStringValue value) {
		featureList.get(featureName).setValue(value);
	}

	public AdditionalUrlInformation newInstance() {
		AdditionalUrlInformation info = new AdditionalUrlInformation();

		for (Additionals key : featureList.keySet()) {
			info.updateValue(key, featureList.get(key).clone().getValue());
		}

		return info;
	}

	@Override
	public int compareTo(AdditionalUrlInformation o) {
		for (Additionals featureName : featureList.keySet()) {
			String thisValue = featureList.get(featureName).getValue()
					.toStringValue();
			String otherValue = o.featureList.get(featureName).getValue()
					.toStringValue();
			if (thisValue.compareTo(otherValue) > 0) {
				if (featureList.get(featureName).isReversal()) {
					return -1;
				} else {
					return 1;
				}
			} else if (thisValue.compareTo(otherValue) < 0) {
				if (featureList.get(featureName).isReversal()) {
					return 1;
				} else {
					return -1;
				}
			}
		}

		return 0;
	}

	public String toString() {
		return featureList.toString();
	}

}
